package com.hotel.BL;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import data.AuditTrail;
import data.Group;
import data.Reservation;


public class AuditTrailLogic extends AbstractBL {
	
	public AuditTrailLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public AuditTrail addGroupAuditTrail(long groupId,String title) throws EntityNotFoundException
	{
		AuditTrail auditTrailItem = new AuditTrail();

		auditTrailItem.setTitle(title);
		auditTrailItem.setStampDate(new Date());
		
		Key k = datastore.put(auditTrailItem.getEntity());
		auditTrailItem.setId(k.getId());
		auditTrailItem.setKey(k);
		
		GroupLogic groupLogic = new GroupLogic();
		Group group = groupLogic.GetGroupById(groupId);
		group.addAuditTrails(auditTrailItem);
		groupLogic.UpdateGroup(group);
		
		return auditTrailItem;
	}
	
	public AuditTrail addAuditTrail(long resId,String title) throws EntityNotFoundException
	{
		AuditTrail auditTrailItem = new AuditTrail();

		auditTrailItem.setTitle(title);
		auditTrailItem.setStampDate(new Date());
		
		Key k = datastore.put(auditTrailItem.getEntity());
		auditTrailItem.setId(k.getId());
		auditTrailItem.setKey(k);
		
		ReservationLogic resvLogic = new ReservationLogic(req,resp);
		Reservation resv = resvLogic.GetReservation(resId);
		resv.addAuditTrails(auditTrailItem);
		resvLogic.UpdateReservation(resv);
		
		return auditTrailItem;
	}
	
	public AuditTrail addAuditTrail(Reservation resv,String title) throws EntityNotFoundException
	{
		AuditTrail auditTrailItem = new AuditTrail();

		auditTrailItem.setTitle(title);
		auditTrailItem.setStampDate(new Date());
		
		Key k = datastore.put(auditTrailItem.getEntity());
		auditTrailItem.setId(k.getId());
		auditTrailItem.setKey(k);
		
		ReservationLogic resvLogic = new ReservationLogic(req,resp);
		resv.addAuditTrails(auditTrailItem);
		resvLogic.UpdateReservation(resv);
		
		return auditTrailItem;
	}
	
	public AuditTrail GetAuditTrail(long id)
	{
		Key key = KeyFactory.createKey(EntityName.AuditTrail, id);
		try {
			Entity lang = datastore.get(key);
			AuditTrail c = new AuditTrail();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<AuditTrail> GetAuditTrailFromResvId(long resId)
	{
		ReservationLogic resvLogic = new ReservationLogic(req,resp);
		
		return resvLogic.GetReservation(resId).getAuditTrailsList();
	}
	
	public List<AuditTrail> GetAuditTrailFromGroupId(long groupId)
	{
		GroupLogic groupLogic = new GroupLogic();
		
		return groupLogic.GetGroupById(groupId).getAuditTrailsList();
	}
}


